package com.cow.sram.module.distribution;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.utils.reflection.BeanToMapUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.dao.UserroleDAO;
import com.cow.sram.data.entity.Roleinfo;
import com.cow.sram.data.entity.Userrole;
import com.cow.sram.data.mapper.RoleinfoMapper;
import com.cow.sram.data.mapper.UserroleMapper;
import com.github.pagehelper.Page;
/**
 * 用户角色
 * @author Administrator
 *
 */
@Service
public class UserroleService extends BaseComponent{
     @Autowired
     UserroleDAO userroleDAO;
 	@Autowired
 	RoleinfoMapper roleinfoMapper;
 	@Autowired
 	UserroleMapper userroleMapper;
     
     /**查带ID*/
     public Userrole getUserroleById(Integer id){
    	 return userroleDAO.findOne(id);
     }
     
     /**增*/
     public Userrole newUserrole(Userrole ur){
    	 return userroleDAO.save(ur);
     }
     
     /**删*/
     public void delUserrole(Integer id){
    	 userroleDAO.delete(id);
     }
     /**根据 userId删除*/
     public void delUserId(Integer userId){
    	 userroleMapper.delUserId(userId);
     }
     
     /**根据 roleId删除*/
     public void delRoleId(Integer roleId){
    	 userroleMapper.delRoleId(roleId);
     }
     
     /**根据 userId删除*/
     public void delUserRole(Integer userId){
    	 Map paramMap = new HashMap();
    	 paramMap.put("userId", userId);
    	 paramMap.put("curUserId", getCurUserId());
    	 userroleMapper.delUserRole(paramMap);
     }
     
     /**用户角色查找*/
 	public Page<Map> findUserRole(Map parmMap){
 		parmMap.put("curUserId", super.getCurUserId());
 		Page<Map> list=roleinfoMapper.queryUserRoleinfo(parmMap);
		for (Map resInfo : list) {
			resInfo.put("roletypeName", dictUtil.getDictValueName("roleType", resInfo.get("roletype").toString()));
		}
		return list;
 	}
     
     /**判断用户是否分配角色*/
     public boolean isExist(Integer userId,Integer roleId){
    	 List list = userroleDAO.findByUseridAndRoleid(userId, roleId);
    	 if(list != null && list.size() > 0){
    		 return true;
    	 }else{
    		 return false;
    	 }
     }
	
}
